/****************************************************************************
**
** Trolltech hereby grants a license to use the Qt/Eclipse Integration
** plug-in (the software contained herein), in binary form, solely for the
** purpose of creating code to be used with Trolltech's Qt software.
**
** Qt Designer is licensed under the terms of the GNU General Public
** License versions 2.0 and 3.0 ("GPL License"). Trolltech offers users the
** right to use certain no GPL licensed software under the terms of its GPL
** Exception version 1.2 (http://trolltech.com/products/qt/gplexception).
**
** THIS SOFTWARE IS PROVIDED BY TROLLTECH AND ITS CONTRIBUTORS (IF ANY) "AS
** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
** OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** Since we now have the GPL exception I think that the "special exception
** is no longer needed. The license text proposed above (other than the
** special exception portion of it) is the BSD license and we have added
** the BSD license as a permissible license under the exception.
**
****************************************************************************/
#include <QtGui>

#include "fuzzysheet.h"
#include "FuzzyTableView.h"

#include "filesave.xpm"
#include "fileopen.xpm"
#include "filenew.xpm"

FuzzySheet::FuzzySheet(QWidget *parent)
    : QMainWindow(parent)
{
	initWindow();
}

FuzzySheet::~FuzzySheet()
{
}

void FuzzySheet::initWindow()
{
	// Setting up MainWindow properties
	setMinimumSize(700,400);
	resize(800,400);
	
	//MENU
	//////////////////////////////////////////////////////////////////////
	
	QAction* pactNew = new QAction("New File", 0);
	pactNew->setText("&New");
    pactNew->setShortcut(QKeySequence("CTRL+N"));
    pactNew->setToolTip("New Document");
    pactNew->setStatusTip("Create a new file");
    pactNew->setWhatsThis("Create a new file");
    pactNew->setIcon(QPixmap(filenew));
    connect(pactNew, SIGNAL(triggered()), SLOT(slotNewDoc()));

    QAction* pactOpen = new QAction("Open File", 0);
    pactOpen->setText("&Open...");
    pactOpen->setShortcut(QKeySequence("CTRL+O"));
    pactOpen->setToolTip("Open Document");
    pactOpen->setStatusTip("Open an existing file");
    pactOpen->setWhatsThis("Open an existing file");
    pactOpen->setIcon(QPixmap(fileopen));
    connect(pactOpen, SIGNAL(triggered()), SLOT(slotLoad()));

    QAction* pactSave = new QAction("Save File", 0);
    pactSave->setText("&Save");
    pactSave->setShortcut(QKeySequence("CTRL+S"));
    pactSave->setToolTip("Save Document");
    pactSave->setStatusTip("Save the file to disk");
    pactSave->setWhatsThis("Save the file to disk");
    pactSave->setIcon(QPixmap(filesave));
    connect(pactSave, SIGNAL(triggered()), SLOT(slotSave()));

    QToolBar* ptbFile = new QToolBar("File Operations");
    ptbFile->addAction(pactNew);
    ptbFile->addAction(pactOpen);
    ptbFile->addAction(pactSave);
    addToolBar(Qt::TopToolBarArea, ptbFile);

    QMenu* pmnuFile = new QMenu("&File");
    pmnuFile->addAction(pactNew);
    pmnuFile->addAction(pactOpen);
    pmnuFile->addAction(pactSave);
    pmnuFile->addAction("Save &As...", this, SLOT(slotSaveAs()));
    pmnuFile->addSeparator();
    pmnuFile->addAction("&Quit", 
                        qApp, 
                        SLOT(closeAllWindows()), 
                        QKeySequence("CTRL+Q")
                       );
    menuBar()->addMenu(pmnuFile);
    m_pmnuWindows = new QMenu("&Windows");
    menuBar()->addMenu(m_pmnuWindows);
    connect(m_pmnuWindows, SIGNAL(aboutToShow()), SLOT(slotWindows()));
    menuBar()->addSeparator();

    QMenu* pmnuHelp = new QMenu("&Help");
    pmnuHelp->addAction("&About", this, SLOT(slotAbout()), Qt::Key_F1);
    menuBar()->addMenu(pmnuHelp);

    m_pws = new QWorkspace;
    m_pws->setScrollBarsEnabled(true);
    setCentralWidget(m_pws);

    m_psigMapper = new QSignalMapper(this);
    connect(m_psigMapper, 
            SIGNAL(mapped(QWidget*)), 
            m_pws, 
            SLOT(setActiveWindow(QWidget*))
           );

    statusBar()->showMessage("Ready", 3000);
}

void FuzzySheet::slotNewDoc()
{
    createNewDoc()->show();
}

// ----------------------------------------------------------------------
CFuzzyTableView* FuzzySheet::createNewDoc(bool fromLoad)
{
	CFuzzyTableView* pdoc = new CFuzzyTableView(fromLoad);
    m_pws->addWindow(pdoc);
    pdoc->setAttribute(Qt::WA_DeleteOnClose);
    pdoc->setWindowTitle("Unnamed Document");
    pdoc->setWindowIcon(QPixmap(filenew));
    connect(pdoc, 
            SIGNAL(changeWindowTitle(const QString&)), 
            SLOT(slotChangeWindowTitle(const QString&))
           );

    return pdoc;
}

// ----------------------------------------------------------------------
void FuzzySheet::slotChangeWindowTitle(const QString& str)
{
    qobject_cast<CFuzzyTableView*>(sender())->setWindowTitle(str);
}

// ----------------------------------------------------------------------
void FuzzySheet::slotLoad()
{
    CFuzzyTableView* pdoc = createNewDoc(true);
    pdoc->slotLoad();
    pdoc->show();
}

// ----------------------------------------------------------------------
void FuzzySheet::slotSave()
{                                                                   
    CFuzzyTableView* pdoc = qobject_cast<CFuzzyTableView*>(m_pws->activeWindow());
    if (pdoc) {
        pdoc->slotSave();
    }
}

// ----------------------------------------------------------------------
void FuzzySheet::slotSaveAs()
{
    CFuzzyTableView* pdoc = qobject_cast<CFuzzyTableView*>(m_pws->activeWindow());
    if (pdoc)
    {
       pdoc->slotSaveAs();
    }
}

// ----------------------------------------------------------------------
void FuzzySheet::slotAbout()
{
    QMessageBox::about(this, "Application", "Fuzzy Networks Calculations");
}

// ----------------------------------------------------------------------
void FuzzySheet::slotWindows()
{
    m_pmnuWindows->clear();

    QAction* pact = m_pmnuWindows->addAction("&Cascade", m_pws, SLOT(cascade()));
    pact->setEnabled(!m_pws->windowList().isEmpty());

    pact = m_pmnuWindows->addAction("&Tile", m_pws, SLOT(tile()));
    pact->setEnabled(!m_pws->windowList().isEmpty());

    m_pmnuWindows->addSeparator();

    QList<QWidget*> listDoc = m_pws->windowList();
    m_pmnuWindows->setEnabled(!listDoc.isEmpty());

    for (int i = 0; i < listDoc.size(); ++i)
    {
        pact = m_pmnuWindows->addAction(listDoc.at(i)->windowTitle());
        pact->setCheckable(true);
        pact->setChecked(m_pws->activeWindow() == listDoc.at(i));
        connect(pact, SIGNAL(triggered()), m_psigMapper, SLOT(map()));
        m_psigMapper->setMapping(pact, listDoc.at(i));
    }
}
